<?php

/**
 * Validator for {@link content}.
 * @see ContentMapper
 */
final class ContentValidator {

    private function __construct() {
        
    }

    /**
     * Validate the given {@link Content} instance.
     * @param Content $content {@link Content} instance to be validated
     * @return array array of {@link Error} s
     */
    public static function validate(Content $content) {
        $errors = array();
        if (!$content->getDate()) {
            $errors[] = new Error('date', 'Empty or invalid date.');
        }
         if (!$content->getLastModifiedOn()) {
            $errors[] = new Error('last_modified_on', 'Empty or invalid last modified date.');
        }
        if (!trim($content->getTitle())) {
            $errors[] = new Error('title', 'Title cannot be empty.');
        }
        if (!trim($content->getText())) {
            $errors[] = new Error('text', 'Text cannot be empty.');
        }

        if (!trim($content->getCategoryId())) {
            $errors[] = new Error('category', 'Category cannot be empty.');
        } elseif (!self::isValidCategory($content->getCategoryId())) {
            $errors[] = new Error('category', 'Invalid category set.');
        }
        return $errors;
    }

    /**
     * Validate the given status.
     * @param string $status status to be validated
     * @throws Exception if the status is not known
     */
    public static function validateCategory($categoryId) {
        if (!self::isValidCategory($categoryId)) {
            throw new Exception('Unknown category: ' . $categoryId);
        }
    }


    private static function isValidCategory($categoryId) {
        return in_array($categoryId, Todo::allStatuses());
    }

}

?>
